package com.jinxinol.basic.system.service;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.springframework.stereotype.Service;

import com.jinxinol.basic.system.domain.SysLogIBName;
import com.jinxinol.basic.system.domain.SysLogInfo;
import com.jinxinol.basic.system.domain.SysUser;
import com.jinxinol.core.common.ContextHelper;
import com.jinxinol.core.util.StringUtils;

@Service
public class LogAspect {
	@Resource
	ISysLogInfoService SysLogInfoService;
	@Resource
	ISysLogIBNameService SysLogInfoBusyNameService;
	
	private Log logger = ContextHelper.getLogger(LogAspect.class);
	public static Map<String, SysLogIBName> busynameMap = new HashMap<String, SysLogIBName>();
	
		public void wirteSuccLog(HttpServletRequest request, String methodName, String busyUrl, long time) throws Exception {
				for(SysLogIBName busyName:SysLogInfoBusyNameService.findByAll()){
					busynameMap.put(busyName.getBusyurl(), busyName);
				}
			    time = System.currentTimeMillis() - time;   
			    SysLogIBName busyName = busynameMap.get(busyUrl);//获取业务类型
			    if(busyName!=null){
					String bType = busyName==null?"未定义业务类型":busyName.getBusyname();
					String successMsg = (busyName==null||StringUtils.isEmpty(busyName.getSuccessmsg()))?"执行请求"+busyUrl+"成功。共花费: " + time + " 毫秒(ms)":busyName.getSuccessmsg();
				    logger.debug(successMsg); 
				    SysLogInfo sLog = new SysLogInfo();
				    SysUser user = null;
				    try{
				    	user = (SysUser) request.getSession().getAttribute("suser");
				    	sLog.setSuid(user.getId().intValue());
				    	sLog.setSuname(user.getUname());
				    	sLog.setSulevel(SysLogInfo.SULEVEL_LEVEL_INFO);
					    sLog.setSumess(bType);
					    sLog.setSuerror(user.getUname()+successMsg);
					    sLog.setMesstype(SysLogInfo.MESSTYPE_RESULT_SUCCESS);
					    sLog.setSuip(request.getLocalAddr());
					    sLog.setSutype(getDoType(methodName));
				    }catch (Exception e) {
				    	sLog.setSuid(0);
				    	sLog.setSuname("");
				    	sLog.setSulevel(SysLogInfo.SULEVEL_LEVEL_INFO);
					    sLog.setSumess(bType);
					    sLog.setSuerror(successMsg);
					    sLog.setMesstype(SysLogInfo.MESSTYPE_RESULT_SUCCESS);
					    sLog.setSuip(request.getLocalAddr());
					    sLog.setSutype(getDoType(methodName));
				    }
				    
				    SysLogInfoService.insert(sLog);
			    }
		}   

		/**
		 * 操作异常时日志
		 * @param jp
		 * @param ex
		 */
		public void wirteFailLog(HttpServletRequest request, String methodName, String busyUrl, long time, Exception ex) {
				try {
					for(SysLogIBName busyName:SysLogInfoBusyNameService.findByAll()){
						busynameMap.put(busyName.getBusyurl(), busyName);
					}
				    time = System.currentTimeMillis() - time;   
				    SysLogIBName busyName = busynameMap.get(busyUrl);//获取业务类型
				    if(busyName!=null){
					    String bType = busyName==null?"未定义业务类型":busyName.getBusyname();
					    String successMsg = (busyName==null||StringUtils.isEmpty(busyName.getFailuremsg()))?"执行请求"+busyUrl+"失败。共花费: " + time + " 毫秒(ms)":busyName.getFailuremsg();
	//					logger.error("执行"+jp.getTarget().getClass().getName()+"类的"+jp.getSignature().getName()+"方法("+bType+")失败。", ex); 
					    logger.error(successMsg); 
					    SysLogInfo sLog = new SysLogInfo();
						SysUser user = null;
					    try{
					    	user = (SysUser) request.getSession().getAttribute("suser");
					    	sLog.setSuid(user.getId().intValue());
					    	sLog.setSuname(user.getUname());
					    	sLog.setSulevel(SysLogInfo.SULEVEL_LEVEL_ERROR);
							sLog.setSumess(bType);
							sLog.setSuerror(ex.getMessage());
							sLog.setMesstype(SysLogInfo.MESSTYPE_RESULT_FAILURE);
							sLog.setSuip(request.getLocalAddr());
							sLog.setSutype(getDoType(methodName));
					    }catch (Exception e) {
					    	sLog.setSuid(0);
					    	sLog.setSuname("");
					    	sLog.setSulevel(SysLogInfo.SULEVEL_LEVEL_ERROR);
							sLog.setSumess(bType);
							sLog.setSuerror(ex.getMessage());
							sLog.setMesstype(SysLogInfo.MESSTYPE_RESULT_FAILURE);
							sLog.setSuip(request.getLocalAddr());
							sLog.setSutype(getDoType(methodName));
					    }
						SysLogInfoService.insert(sLog);
				    }
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
		public int getDoType(String methodName){
			if(methodName.toLowerCase().startsWith("insert")){
				return SysLogInfo.SUTYPE_TYPE_INSERT;
			}else if(methodName.toLowerCase().startsWith("delete")){
				return SysLogInfo.SUTYPE_TYPE_DELETE;
			}else if(methodName.toLowerCase().startsWith("query")||methodName.toLowerCase().startsWith("sel")){
				return SysLogInfo.SUTYPE_TYPE_QUERY;
			}else if(methodName.toLowerCase().startsWith("update")){
				return SysLogInfo.SUTYPE_TYPE_UPDATE;
			}else{
				return SysLogInfo.SUTYPE_TYPE_UNKNOW;
			}
		}
}
